/**
 * Created by lwy on 2017/5/27.
 */
var verifyCode;
var twice = false;

document.getElementById('verify_certain').addEventListener('tap',function () {
    $.ajax({
        //验证码是否正确接口
        url:'/imageVerifyC/verifyCode.json',
        dataType:'json',
        data:{
            mobile:$('#phone').val(),
            validateCode:$('#code_input').val(),
            companyCode:getItem('companyCode'),
        },
        type:'post',
        success:function (data) {
            if(data.code!=1){
                mui.toast(data.msg);
                return;
            }
            if(data.code==1){
                if(data.verifyCode){
                    $('.zhezhao').hide();
                    twice=true;
                    verifyCode = $('#code_input').val();
                    setItem('verifyCode',$('#code_input').val());
                    verify();
                }else{
                    mui.toast('验证码错误');
                    $('#code_input').val('');
                    var timestamp = (new Date()).valueOf();
                    $("#imgObj").attr("src","/imageVerifyC/validateCode.json?mobile="+$('#phone').val()+"&t="+timestamp);
                    return;
                }
            }
        }
    })
})


//关闭验证窗口
$('#close').click(function () {
    $('.zhezhao').hide();
})

//发送验证码
$('.code-button').click(function(){
    sendCode();
})
function sendCode(){
    if(!ifCyber()){
        mui.toast('请检查网络重试');
    }

    //验证该手机号是否发送过验证码
    $.ajax({
        url:'/imageVerifyC/ifCode.json',
        dataType:'json',
        data:{
            mobile:$('#phone').val(),
            companyCode:getItem('companyCode'),
        },
        type:'post',
        success:function (data) {
            if(data.code==1){
                if(data.ifCode){
                    //如果为true，从本地获取图片验证码
                    verifyCode = getItem('verifyCode');
                    verify();
                }else{
                    //如果为false，需要输入
                    var timestamp = (new Date()).valueOf();
                    $("#imgObj").attr("src","/imageVerifyC/validateCode.json?mobile="+$('#phone').val()+"&t="+timestamp);
                    $('#code_input').val('');
                    $('.zhezhao').show();
                }
            }
            if(data.code==0){
                mui.toast(data.msg);
                return
            }
        }
    })

}

function verify() {
    $(".code-button").html("发送中…");
    //$(".get-code").unbind();
    $.ajax({
        url:'/user/sendMobileCode_ValidateCode.json',
        data:{
            platform:'wap',
            mobile:$('#phone').val(),
            validateCode:verifyCode,
            companyCode:getItem('companyCode'),
        },
        dataType:'json',
        success:function(data) {
            if(data.code!=1){
                mui.toast(data.msg);
                $(".code-button").html("重新发送");
                //$(".get-code").bind('click',sendCode);
                return;
            }
            timeUpdate.init($(".code-button"), 60);

        }
    })
}

var timeUpdate = {
    node:null,//节点
    time:60,//时间
    //主函数
    start:function(){
        if(this.time > 0){
            $(this.node).html( this.time-- + 's后重发');
            var _this = this;
            setTimeout(function(){
                _this.start();
            },1000);
        }else{
            this.node.click(function () {
                sendCode();
            });
            this.node.html("重新发送");
        }
    },
    //初始化
    init:function(node, timeLimit){
        this.node = node;
        this.time = timeLimit;
        this.start();
    }
};


//确定
$('.set-certain').click(function(){
    var userName = $('#phone').val();
    var passWord = $('#conpassword').val();
    if(passWord==""||conpassword==""){
        mui.toast('密码不能为空');
        return;
    }
    if(!ifCyber()){
        mui.toast('请检查网络重试');
    }
    $.ajax({
        url:'/user/userLoginKey.json',
        dataType:'json',
        data:{
            name:userName,
            companyCode:getItem('companyCode'),
        },
        success:function(data){
            if(data.code!=1){
                mui.toast(data.msg);
                return;
            }
            if(data.code==1){
                passWord= calcMD5(passWord + data.datas.userKey, 32);
                $.ajax({
                    url:'/user/findPassword.json',
                    dataType:'json',
                    type:'post',
                    data:{
                        password:passWord,
                        mobile_code:$('.code-input').val(),
                        platform:'wap',
                        phone:userName,
                        companyCode:getItem('companyCode'),
                    },
                    success:function(data){
                        if(data.code!=1){
                            mui.toast(data.msg);
                            return;
                        }
                        if(data.code==1){
                            removeItem('token');
                            removeItem('phone');
                            removeItem('passWord');
                            window.location.href="login.html";
                        }
                    }
                })
            }
        }
    })
})